--    Program: 		CAPRCVIMP_SHIPMENT_LINES.sql
--    Description:  Import RCV SHIPMENT LINES information 
--    Revision:     1st     
--    Parameter: 	
--    Created By:   Kenny Leung
--    Date:         24-Jan-2006

set doc off;      
set verify off;
set serveroutput on size 1000000;
whenever sqlerror exit failure rollback;

declare   
   l_processed_count		number;
   l_rejected_count 		number;
   l_err_msg				varchar2(2000);
   l_err_msg_tmp			varchar2(2000);

   l_shipment_line_id       number;
                    		                 		
   cursor cur_shipment_lines is
      select * from CRST_RCV_SHIPMENT_LINES for update;

begin
   l_processed_count := 0;
   l_rejected_count  := 0;
   l_err_msg := '';
   
   for cur in cur_shipment_lines loop   	
     l_err_msg_tmp := '';
     
     select RCV_SHIPMENT_LINES_S.NEXTVAL 
     into l_shipment_line_id
     FROM DUAL;
     
     update CRST_RCV_SHIPMENT_LINES
     set    shipment_line_id_n = l_shipment_line_id
     where  shipment_line_id = cur.SHIPMENT_LINE_ID;
     
     begin
       -- import data
       insert into RCV_SHIPMENT_LINES
       (
            SHIPMENT_LINE_ID              ,
            LAST_UPDATE_DATE              ,
            LAST_UPDATED_BY               ,
            CREATION_DATE                 ,
            CREATED_BY                    ,
            LAST_UPDATE_LOGIN             ,
            SHIPMENT_HEADER_ID            ,
            LINE_NUM                      ,
            CATEGORY_ID                   ,
            QUANTITY_SHIPPED              ,
            QUANTITY_RECEIVED             ,
            UNIT_OF_MEASURE               ,
            ITEM_DESCRIPTION              ,
            ITEM_ID                       ,
            ITEM_REVISION                 ,
            VENDOR_ITEM_NUM               ,
            VENDOR_LOT_NUM                ,
            UOM_CONVERSION_RATE           ,
            SHIPMENT_LINE_STATUS_CODE     ,
            SOURCE_DOCUMENT_CODE          ,
            PO_HEADER_ID                  ,
            PO_RELEASE_ID                 ,
            PO_LINE_ID                    ,
            PO_LINE_LOCATION_ID           ,
            PO_DISTRIBUTION_ID            ,
            REQUISITION_LINE_ID           ,
            REQ_DISTRIBUTION_ID           ,
            ROUTING_HEADER_ID             ,
            PACKING_SLIP                  ,
            FROM_ORGANIZATION_ID          ,
            DELIVER_TO_PERSON_ID          ,
            EMPLOYEE_ID                   ,
            DESTINATION_TYPE_CODE         ,
            TO_ORGANIZATION_ID            ,
            TO_SUBINVENTORY               ,
            LOCATOR_ID                    ,
            DELIVER_TO_LOCATION_ID        ,
            CHARGE_ACCOUNT_ID             ,
            TRANSPORTATION_ACCOUNT_ID     ,
            SHIPMENT_UNIT_PRICE           ,
            TRANSFER_COST                 ,
            TRANSPORTATION_COST           ,
            COMMENTS                      ,
            ATTRIBUTE_CATEGORY            ,
            ATTRIBUTE1                    ,
            ATTRIBUTE2                    ,
            ATTRIBUTE3                    ,
            ATTRIBUTE4                    ,
            ATTRIBUTE5                    ,
            ATTRIBUTE6                    ,
            ATTRIBUTE7                    ,
            ATTRIBUTE8                    ,
            ATTRIBUTE9                    ,
            ATTRIBUTE10                   ,
            ATTRIBUTE11                   ,
            ATTRIBUTE12                   ,
            ATTRIBUTE13                   ,
            ATTRIBUTE14                   ,
            ATTRIBUTE15                   ,
            REASON_ID                     ,
            USSGL_TRANSACTION_CODE        ,
            GOVERNMENT_CONTEXT            ,
            REQUEST_ID                    ,
            PROGRAM_APPLICATION_ID        ,
            PROGRAM_ID                    ,
            PROGRAM_UPDATE_DATE           ,
            DESTINATION_CONTEXT           ,
            PRIMARY_UNIT_OF_MEASURE       ,
            EXCESS_TRANSPORT_REASON       ,
            EXCESS_TRANSPORT_RESPONSIBLE  ,
            EXCESS_TRANSPORT_AUTH_NUM     ,
            ASN_LINE_FLAG                 ,
            ORIGINAL_ASN_PARENT_LINE_ID   ,
            ORIGINAL_ASN_LINE_FLAG        ,
            VENDOR_CUM_SHIPPED_QUANTITY   ,
            NOTICE_UNIT_PRICE             ,
            TAX_NAME                      ,
            TAX_AMOUNT                    ,
            INVOICE_STATUS_CODE           ,
            CUM_COMPARISON_FLAG           ,
            CONTAINER_NUM                 ,
            TRUCK_NUM                     ,
            BAR_CODE_LABEL                ,
            TRANSFER_PERCENTAGE           ,
            MRC_SHIPMENT_UNIT_PRICE       ,
            MRC_TRANSFER_COST             ,
            MRC_TRANSPORTATION_COST       ,
            MRC_NOTICE_UNIT_PRICE         ,
            SHIP_TO_LOCATION_ID           ,
            COUNTRY_OF_ORIGIN_CODE        ,
            OE_ORDER_HEADER_ID            ,
            OE_ORDER_LINE_ID              ,
            CUSTOMER_ITEM_NUM             ,
            COST_GROUP_ID                 ,
            SECONDARY_QUANTITY_SHIPPED    ,
            SECONDARY_QUANTITY_RECEIVED   ,
            SECONDARY_UNIT_OF_MEASURE     ,
            QC_GRADE                      
       )
       values
       (
            l_shipment_line_id                ,
            cur.LAST_UPDATE_DATE              ,
            CUR.LAST_UPDATED_BY,
            cur.CREATION_DATE                 ,
            CAPBRIMP_MAPPING.GetUserMapping(cur.CREATED_BY),
            cur.LAST_UPDATE_LOGIN             ,
            CAPPOIMP_MAPPING.GetReceiptHeaderIDMapping(cur.SHIPMENT_HEADER_ID),
            cur.LINE_NUM                      ,
            cur.CATEGORY_ID                   ,
            cur.QUANTITY_SHIPPED              ,
            cur.QUANTITY_RECEIVED             ,
            cur.UNIT_OF_MEASURE               ,
            cur.ITEM_DESCRIPTION              ,
            --CAPPOIMP_MAPPING.GetItemIDMapping(cur.TO_ORGANIZATION_ID, cur.ITEM_ID),
            CAPPOIMP_MAPPING.GetItemIDMapping(cur.ITEM_ID),
            cur.ITEM_REVISION                 ,
            cur.VENDOR_ITEM_NUM               ,
            cur.VENDOR_LOT_NUM                ,
            cur.UOM_CONVERSION_RATE           ,
            cur.SHIPMENT_LINE_STATUS_CODE     ,
            cur.SOURCE_DOCUMENT_CODE          ,
            CAPPOIMP_MAPPING.GetPOHeaderIDMapping(cur.PO_HEADER_ID),
            cur.PO_RELEASE_ID                 , --NULL
            CAPPOIMP_MAPPING.GetPOLineIDMapping(cur.PO_LINE_ID),
            CAPPOIMP_MAPPING.GetPOLineLocationIDMapping(cur.PO_LINE_LOCATION_ID),
            CAPPOIMP_MAPPING.GetPODistributionIDMapping(cur.PO_DISTRIBUTION_ID),
            CAPPOIMP_MAPPING.GetPRLineIDMapping(cur.REQUISITION_LINE_ID),
            CAPPOIMP_MAPPING.GetPRDistributionIDMapping(cur.REQ_DISTRIBUTION_ID),
            cur.ROUTING_HEADER_ID             ,
            cur.PACKING_SLIP                  ,
            CAPBRIMP_MAPPING.GetOrganizationMapping(cur.FROM_ORGANIZATION_ID),
            CAPBRIMP_MAPPING.GetEmployeeMapping(cur.DELIVER_TO_PERSON_ID),
            CAPBRIMP_MAPPING.GetEmployeeMapping(cur.EMPLOYEE_ID),
            cur.DESTINATION_TYPE_CODE         ,
            CAPBRIMP_MAPPING.GetOrganizationMapping(cur.TO_ORGANIZATION_ID), 
            cur.TO_SUBINVENTORY               ,
            cur.LOCATOR_ID                    , --NULL
            CAPPOIMP_MAPPING.GetHRLocationIDMapping(cur.DELIVER_TO_LOCATION_ID), 
            CAPBRIMP_MAPPING.GetCCIDMapping(cur.CHARGE_ACCOUNT_ID),
            CAPBRIMP_MAPPING.GetCCIDMapping(cur.TRANSPORTATION_ACCOUNT_ID),
            cur.SHIPMENT_UNIT_PRICE           ,
            cur.TRANSFER_COST                 ,
            cur.TRANSPORTATION_COST           ,
            cur.COMMENTS                      ,
            cur.ATTRIBUTE_CATEGORY            ,
            cur.ATTRIBUTE1                    ,
            cur.ATTRIBUTE2                    ,
            cur.ATTRIBUTE3                    ,
            cur.ATTRIBUTE4                    ,
            cur.ATTRIBUTE5                    ,
            cur.ATTRIBUTE6                    ,
            cur.ATTRIBUTE7                    ,
            cur.ATTRIBUTE8                    ,
            cur.ATTRIBUTE9                    ,
            cur.ATTRIBUTE10                   ,
            cur.ATTRIBUTE11                   ,
            cur.ATTRIBUTE12                   ,
            cur.ATTRIBUTE13                   ,
            cur.ATTRIBUTE14                   ,
            cur.ATTRIBUTE15                   ,
            cur.REASON_ID                     ,
            cur.USSGL_TRANSACTION_CODE        ,
            cur.GOVERNMENT_CONTEXT            ,
            cur.REQUEST_ID                    ,
            cur.PROGRAM_APPLICATION_ID        ,
            cur.PROGRAM_ID                    ,
            cur.PROGRAM_UPDATE_DATE           ,
            cur.DESTINATION_CONTEXT           ,
            cur.PRIMARY_UNIT_OF_MEASURE       ,
            cur.EXCESS_TRANSPORT_REASON       ,
            cur.EXCESS_TRANSPORT_RESPONSIBLE  ,
            cur.EXCESS_TRANSPORT_AUTH_NUM     ,
            cur.ASN_LINE_FLAG                 ,
            cur.ORIGINAL_ASN_PARENT_LINE_ID   ,
            cur.ORIGINAL_ASN_LINE_FLAG        ,
            cur.VENDOR_CUM_SHIPPED_QUANTITY   ,
            cur.NOTICE_UNIT_PRICE             ,
            cur.TAX_NAME                      ,
            cur.TAX_AMOUNT                    ,
            cur.INVOICE_STATUS_CODE           ,
            cur.CUM_COMPARISON_FLAG           ,
            cur.CONTAINER_NUM                 ,
            cur.TRUCK_NUM                     ,
            cur.BAR_CODE_LABEL                ,
            cur.TRANSFER_PERCENTAGE           ,
            cur.MRC_SHIPMENT_UNIT_PRICE       ,
            cur.MRC_TRANSFER_COST             ,
            cur.MRC_TRANSPORTATION_COST       ,
            cur.MRC_NOTICE_UNIT_PRICE         ,
            CAPPOIMP_MAPPING.GetHRLocationIDMapping(cur.SHIP_TO_LOCATION_ID),
            cur.COUNTRY_OF_ORIGIN_CODE        ,
            cur.OE_ORDER_HEADER_ID            ,
            cur.OE_ORDER_LINE_ID              ,
            cur.CUSTOMER_ITEM_NUM             ,
            cur.COST_GROUP_ID                 ,
            cur.SECONDARY_QUANTITY_SHIPPED    ,
            cur.SECONDARY_QUANTITY_RECEIVED   ,
            cur.SECONDARY_UNIT_OF_MEASURE     ,
            cur.QC_GRADE                      
       );  
     exception
       when others then
     	 l_err_msg_tmp := sqlerrm;
     end;	  	    
     
     if l_err_msg_tmp IS NULL then
       l_processed_count := l_processed_count + 1;  
     else
       l_rejected_count := l_rejected_count + 1;
       l_err_msg := l_err_msg_tmp; 
     end if;
   end loop;

   if l_rejected_count = 0 then
   	 commit;
   else
     rollback;
   end if;
   
   -- insert into log table
   insert into capcnv_log ( prog_id,process_date, record_processed,record_rejected,record_extracted,err_msg) 
    values ('CAPPOIMP_RECEIPT_LINE', sysdate, l_processed_count, l_rejected_count, l_processed_count-l_rejected_count, l_err_msg );
   
   commit;
exception 
  when others then
    dbms_output.put_line(sqlerrm);
    rollback;  
end;
/


--exit
